import { generateCaptcha } from '@/utils/backend/captcha'
import { HttpStatus } from '@/utils/httpStatus'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'

export const GET = async () => {
	try {
		const { code, svg } = generateCaptcha()

		const sessionCookies = await cookies()
		sessionCookies.set('captcha', code, {
			httpOnly: true,
			secure: process.env.NODE_ENV === 'production',
			sameSite: 'strict',
			maxAge: 300, // 5分钟
		})

		return NextResponse.json({
			success: true,
			message: '获取验证码成功',
			data: svg,
		})
	} catch (err) {
		console.error('生成验证码失败:', err)
		return NextResponse.json(
			{
				success: false,
				message: '生成验证码失败',
				data: null,
			},
			{ status: HttpStatus.INTERNAL_SERVER_ERROR }
		)
	}
}
